package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.TPayChannel;
import com.ruoyi.business.domain.TUserChannel;
import com.ruoyi.business.service.ITPayChannelService;
import com.ruoyi.business.service.ITUserChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付通道Controller
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Controller
@RequestMapping("/business/channel")
public class TPayChannelController extends BaseController
{
    private String prefix = "business/channel";

    @Autowired
    private ITPayChannelService tPayChannelService;

    @Autowired
    private ITUserChannelService itUserChannelService;


    @RequiresPermissions("business:channel:view")
    @GetMapping()
    public String channel(ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser.getMerchantType().equals("0") || sysUser.getMerchantType().equals("2")){
            mmap.put("isShow",false);
        }else {
            mmap.put("isShow",true);
        }
        return prefix + "/channel";
    }

    @GetMapping("/doc")
    public String doc()
    {
        return prefix + "/doc";
    }

    /**
     * 查询支付通道列表
     */
    @RequiresPermissions("business:channel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPayChannel tPayChannel)
    {
        startPage();
        boolean isAdmin = ShiroUtils.getSysUser().isAdmin();
        if(isAdmin){
            return getDataTable(tPayChannelService.selectTPayChannelList(tPayChannel));
        }
        tPayChannel.setUserId(ShiroUtils.getUserId());
        List<TPayChannel> list = tPayChannelService.selectTPayChannelListByUser(tPayChannel);
        return getDataTable(list);
    }

    /**
     * 导出支付通道列表
     */
    @RequiresPermissions("business:channel:export")
    @Log(title = "支付通道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TPayChannel tPayChannel)
    {
        List<TPayChannel> list = tPayChannelService.selectTPayChannelList(tPayChannel);
        ExcelUtil<TPayChannel> util = new ExcelUtil<TPayChannel>(TPayChannel.class);
        return util.exportExcel(list, "支付通道数据");
    }

    /**
     * 新增支付通道
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存支付通道
     */
    @RequiresPermissions("business:channel:add")
    @Log(title = "支付通道", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TPayChannel tPayChannel)
    {
        if(tPayChannel.getLimitPerMinMoney()!=null)
            tPayChannel.setLimitPerMinMoney(tPayChannel.getLimitPerMinMoney()*100);
        if(tPayChannel.getLimitPerMaxMoney()!=null)
            tPayChannel.setLimitPerMaxMoney(tPayChannel.getLimitPerMaxMoney()*100);
        return toAjax(tPayChannelService.insertTPayChannel(tPayChannel));
    }

    /**
     * 修改支付通道
     */
    @RequiresPermissions("business:channel:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TPayChannel tPayChannel = tPayChannelService.selectTPayChannelById(id);
        mmap.put("tPayChannel", tPayChannel);
        return prefix + "/edit";
    }

    /**
     * 修改保存支付通道
     */
    @RequiresPermissions("business:channel:edit")
    @Log(title = "支付通道", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TPayChannel tPayChannel)
    {
        if(tPayChannel.getLimitPerMinMoney()!=null)
            tPayChannel.setLimitPerMinMoney(tPayChannel.getLimitPerMinMoney()*100);
        if(tPayChannel.getLimitPerMaxMoney()!=null)
            tPayChannel.setLimitPerMaxMoney(tPayChannel.getLimitPerMaxMoney()*100);
        return toAjax(tPayChannelService.updateTPayChannel(tPayChannel));
    }

    /**
     * 删除支付通道
     */
    @RequiresPermissions("business:channel:remove")
    @Log(title = "支付通道", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tPayChannelService.deleteTPayChannelByIds(ids));
    }


//    @RequiresPermissions("business:channel:list")
    @PostMapping("/getChannel/{type}")
    @ResponseBody
    public AjaxResult getChannel(@PathVariable("type") String type)
    {
        boolean isAdmin = ShiroUtils.getSysUser().isAdmin();
        TPayChannel tPayChannel = new TPayChannel();
        if(isAdmin){
            tPayChannel.setType(type);
            return AjaxResult.success(tPayChannelService.selectTPayChannelList(tPayChannel));
        }
        tPayChannel.setType(type);
        tPayChannel.setUserId(ShiroUtils.getUserId());
        List<TPayChannel> list = tPayChannelService.selectTPayChannelListByUser(tPayChannel);
        return AjaxResult.success(list);
    }

    @PostMapping("/getChannelByCode/{code}")
    @ResponseBody
    public AjaxResult getChannelByCode(@PathVariable("code") String code)
    {
        boolean isAdmin = ShiroUtils.getSysUser().isAdmin();
        TPayChannel tPayChannel = new TPayChannel();
        if(isAdmin){

            tPayChannel.setChannel(code);
            return AjaxResult.success(tPayChannelService.selectTPayChannelList(tPayChannel).get(0));
        }

        tPayChannel.setChannel(code);
        tPayChannel.setUserId(ShiroUtils.getUserId());
        List<TPayChannel> list = tPayChannelService.selectTPayChannelListByUser(tPayChannel);
        if(list != null && list.size() > 0){
            return AjaxResult.success(list.get(0));
        }
        return AjaxResult.error("未找到通道");
    }

    /**
     * 修改支付通道
     */
    @RequiresPermissions("business:channel:operstatus")
    @GetMapping("/weightSet/{id}")
    public String weightSet(@PathVariable("id") Long id, ModelMap mmap)
    {
        TPayChannel tPayChannel = tPayChannelService.selectTPayChannelById(id);
        mmap.put("tPayChannel", tPayChannel);
        return prefix + "/ratioCfg";
    }
    @RequiresPermissions("business:channel:list")
    @PostMapping("/ratioCfglist")
    @ResponseBody
    public TableDataInfo ratioCfglist(TUserChannel tUserChannel){
        startPage();
        List<TUserChannel> list = itUserChannelService.selectTUserChannelList(tUserChannel);
        return getDataTable(list);
    }

    @RequiresPermissions("business:channel:operstatus")
    @PostMapping("/ratioCfg")
    @ResponseBody
    public AjaxResult ratioCfg(TUserChannel tUserChannel){
       TUserChannel oldTUserChannel = itUserChannelService.selectTUserChannelById(tUserChannel.getId());
        TUserChannel qry = new TUserChannel();
        qry.setChannel(oldTUserChannel.getChannel());
        qry.setMerchantType(tUserChannel.getMerchantType());
        List<TUserChannel> userChannelList =  itUserChannelService.selectTUserChannelList(qry);
        int ratioCfg = 0;
        for (TUserChannel userChannel : userChannelList) {
            if(userChannel.getId() != oldTUserChannel.getId()){
                ratioCfg += userChannel.getRatio()==null?0:userChannel.getRatio();
            }
        }
        if((ratioCfg + tUserChannel.getRatio() )>100){
            return AjaxResult.error("权重设置失败,同类型的权重已经超过100%");
        }
        oldTUserChannel.setRatio(tUserChannel.getRatio());
        itUserChannelService.updateTUserChannel(oldTUserChannel);
        return AjaxResult.success("权重设置成功");
    }

    @Log(title = "通道状态变更", businessType = BusinessType.UPDATE)
    @RequiresPermissions("business:channel:operstatus")
    @PostMapping("/changeChannelDisabledStatus")
    @ResponseBody
    public AjaxResult changeChannelDisabledStatus(TPayChannel tPayChannel) {
        TPayChannel newTPayChannel = tPayChannelService.selectTPayChannelById(tPayChannel.getId());
        newTPayChannel.setDisabled(tPayChannel.getDisabled());
        return toAjax(tPayChannelService.updateTPayChannel(newTPayChannel));
//        return toAjax(tNginxInfoService.updateTNginxInfo(tNginxInfo));
    }
}
